<?php
session_start();
include_once("../service/productService.php");
include_once("../service/categoryService.php");
include_once("../util/constant.php");
include_once("../util/functionUtil.php");
include_once("../util/FTP.php");

$catId = $_POST["category"];
$disId = $_POST["discount"];
$name = $_POST["name"];
$code = $_POST["code"];
$visibility = $_POST["visibility"];
$status = $_POST["status"];
$price = $_POST["price"];
$description = $_POST["description"];
$attributes = isset($_POST["attributes"]) ? ($_POST["attributes"]) : "";
$attributeIds = isset($_POST["attIds"]) ? array_values($_POST["attIds"]) : "";
$optionalPrices = isset($_POST["optionalPrices"]) ? array_values($_POST["optionalPrices"]) : "";
$optionalPriceIds = isset($_POST["oppIds"]) ? array_values($_POST["oppIds"]) : "";
$imgPaths = $_POST["imgPaths"];

if(isset($catId) && isset($name) && isset($code) && isset($visibility) && isset($price)){
    $proId = createProduct($disId, $catId, $status, $code, $name, $visibility, $price, $description);    
}else{
    echo "<script>window.location.href='../admin/product-detail.php?action=".NEW_ACTION."';</script>";
}

if($proId == 0) "<script>window.location.href='../admin/product-detail.php?action=".INSERT_FAILED;
else {
    // check if the product has attributes and attributes' ids or not
    if(!(empty($attributes) || 
         empty($attributeIds) || 
        (count($attributes) != count($attributeIds)))){
        for($i = 0; $i < count($attributes); $i++){
            if(createAttributeValue(findCategoryByAttribute($attributeIds[$i]), $attributeIds[$i], $proId, $i, $attributes[$i]) == 0)
                echo "<script>window.location.href='../admin/product-detail.php?action='.INSERT_FAILED.'.1&id='.$proId.';</script>";
        }
    }
    
    // check if the product has optional prices and optional prices' ids or not
    if(count($optionalPriceIds) > 0
        /*!(empty($optionalPrices) || 
         empty($optionalPriceIds) || 
        (count($optionalPrices) != count($optionalPriceIds)))*/
        ){
       for($i = 0; $i < count($optionalPriceIds); $i++){
            if(createOptionalValue(findCategoryByOptionalPrice($optionalPriceIds[$i]), $optionalPriceIds[$i], $proId, 0/*$optionalPrices[$i]*/) == 0)
            	echo "<script>window.location.href = '../admin/product-detail.php?action=".INSERT_FAILED.".2&id=".$proId."';</script>";
       }    
    }
    
    // process uploaded images
    //if($imgPaths != ""){
		if(IS_ON_SERVER) FTP::getInstance()->ftpMakeDir(FTP::getInstance()->getServerPath()."product/".$proId, 0777);
    	else mkdir("../image/product/".$proId, 0777);
//        $arrayOfImagePath = preg_split("/,/", $imgPaths);
//        
//        foreach($arrayOfImagePath as $each){
//            if(creatNormalImage($proId, $each) == 0)
//            	echo "<script>window.location.href = '../admin/product-detail.php?action=".INSERT_FAILED.".3&id=".$proId."';</script>";    
//        }
//    }
}

echo "<script>window.location.href = '../admin/product-detail.php?action=".INSERT_SUCCESSFULLY."&id=".$proId."';</script>";
?>